# $Id: PKGBUILD 71695 2012-06-01 13:05:36Z dreisner $
# Maintainer: Sergej Pupykin <pupykin.s+arch@gmail.com>
# Contributor: Brad Fanella <bradfanella@archlinux.us>
# Contributor: [vEX] <niechift.dot.vex.at.gmail.dot.com>
# Contributor: Zeqadious <zeqadious.at.gmail.dot.com>
# Contributor: BlackIkeEagle < ike DOT devolder AT gmail DOT com >

# ALARM:
#  - --disable-vdpau/xvda, remove from makedepends
#  - specify our C/CXXFLAGS in configure
#  - removed --disable-external-ffmpeg
#  - disable gl and vaapi. enabled gles and with-platform=marvell-dove
#  - replace libcec with libcec-cubox
#  - additional depends xf86-video-dove, marvell-ipp, ffmpeg
#  - added vmeta-clk@667.service requires to systemd sevice
#  - Use GeeXboX/SolidRun patcheset

buildarch=4

pkgname=("xbmc-cubox")
provides=("xbmc")
conflicts=("xbmc" "xbmc-cubox-git")
replaces=("xbmc-cubox-git")

_patches=(
      "0001-dove-added-vmeta-library-loader.patch"
      "0002-dove-added-vmeta-video-decoder.patch"
      "0003-dove-added-dove-overlay-render.patch"
      "0004-dove-hook-up-vmeta-video-decoder.patch"
      "0005-dove-hook-up-dove-overlay-renderer.patch"
      "0006-dove-workaround-for-eglCreateContext.patch"
      "0008-dove-missing-typdef-leads-in-XKBlib.h-include-errors.patch"
      "0009-dove-hook-up-dove-support-in-the-xbmc-build-system.patch"
      "0011-dove-changed-vblank-waiting-is-only-needed-for-DOVEF.patch"
      "0014-dove-workaround-for-marvell-dove.-EGL_NO_CONTEXT-cau.patch"
      "0015-dove-fixed-typos.patch"
      "0017-dove-fixed-exit-hang.-this-is-a-workaround-for-a-bug.patch"
      "0018_add-codecs.patch"
      "0019_gpu-backoff.patch"
      "0020_use-ffmpeg-for-thumbnails.patch"
      "0021_big-buck-bunny-480p-fix.patch"
      "0032-Sync-frame-directly-on-next-vsync.patch"
      "0033-Cached-buffer-allocation.patch"
      "0034-Vmeta-queuing-support.patch"
      "0035-Add-the-_cached-and-_writecombine-function-APIs.patch"
      "0036-vsync-always-enabled.patch"
      #"0037-Select-vmeta-speed.patch"
      #"0038-Make-vmeta-clock-settings-correct-when-loading-xml.patch"
      "0039_WinSystemX11-SDL_CreateRGBSurfaceFrom.patch"
      "0040-Reworked-iLineSize-detection.patch"
      "0040-hack.patch"
      #"0041-Make-vmeta-clock-default-to-667MHz.patch"
      "0042-mpeg4-packed-bitstream-unpacking.patch"
      "0043-mpeg2-aspect-ratio-detection.patch"
      "0045-performance-bitstream-converter.patch"
      "0046-performance-vmeta-decoder.patch"
      "0047-performance-overlay-renderer.patch"
      "0048-drop-first-two-frames-from-vmeta.patch"
      "0049-overlay-renderer-streamlined.patch"
      #"0050-Use-Dove-hardware-graphics-scaler.patch"
      #"0051-Graphics-scaling-text-English.patch"
      "0052-Fixed-vmeta-cache-coherency-problem.patch"
      "0052-Remove-enabling-vertical-interpolation-smoothing-in-.patch"
      "0053-Configure-upscaling-also-on-refresh-adjustment.patch"
      "0054-Remove-unsupported-audio-settings.patch"
      "0055-vmeta-used-for-mpeg1.patch"
      "0056-dove-overlay-setup-fix.patch"
      #"0057-graphics-scaling-set-default-to-x1.patch"
      #"0058-vmeta-set-clock-only-when-inuse.patch"
      "0059-graphics-scaling-cleanup-and-subtitle-pos-fix.patch"
      "0060-audioengine-fix-24bit-pcm-output.patch"
      "0061-audioengine-improve-saferound.patch"
      "0062-vmeta-use-writecombine-alloc.patch"
      "0063-vmeta-detect-mp4-flavor.patch"
      "0064-vmeta-fix-vc1-playback.patch"
      "0065-vmeta-used-for-wmv3.patch"
      "0066-vmeta-cleanups-after-wmv3.patch"
      "0067-dove-overlay-alignment-fix.patch"
      "0068-vmeta-avoid-compiler-warnings.patch"
      "0069-overlay-renderer-more-video-settings.patch"
      "0070-graphics-scaling-switch-interpolation.patch"
      "0071-vmeta-dont-decode-when-draining-queue.patch"
      "0072-FLACcodec-improve-performance.patch"
      "0073-mathutils-speedup-attempt.patch"
      "0074_fix-mpeg1-aspect-ratio.patch"
      "0075_avoid-flickering-line-hack.patch"
      "0076-audioengine-do-not-transcode-mono.patch"
      "0077-vmeta-count-submitted-buffers.patch"
      "0078-vmeta-skip-stuffing-byte.patch"
      #"0079_use-vmeta-for-jpeg-decoding-v4.patch"
      #"505_report-cpu-type.diff"
)

_prefix=/usr

pkgver=12.3
pkgrel=1
pkgdesc="A software media player and entertainment hub for digital media"
arch=('i686' 'x86_64')
url="http://xbmc.org"
license=('GPL' 'custom')
depends=('libpulse' 'hicolor-icon-theme' 'fribidi' 'lzo2' 'smbclient' 'libtiff' 'libva'
	 'libpng' 'libcdio' 'yajl' 'libmariadbclient' 'libjpeg-turbo' 'libsamplerate'
	 'glew' 'libssh' 'libmicrohttpd' 'libxrandr' 'sdl_mixer' 'sdl_image' 'python2'
	 'libass' 'libmpeg2' 'libmad' 'libmodplug' 'jasper' 'rtmpdump' 'unzip' 'mesa-demos'
	 'xorg-xdpyinfo' 'libbluray' 'libnfs' 'afpfs-ng' 'libshairport' 'avahi' 'bluez-libs' 'glu'
	 'tinyxml' 'taglib' 'xf86-video-dove' 'marvell-ipp' 'ffmpeg' 'xorg-xinit' 'xorg-server' 'libcec-cubox')
makedepends=('boost' 'cmake' 'gperf' 'nasm' 'libxinerama' 'zip' 'libcec-cubox'
	     'udisks' 'upower' 'mesa' 'doxygen' 'swig' 'java-environment')
optdepends=('pulseaudio: pulseaudio support'
	    'lirc: remote controller support'
	    'udisks: automount external drives'
	    'upower: used to trigger suspend functionality'
	    'unrar: access compressed files without unpacking them')
install="xbmc.install"
source=("http://mirrors.xbmc.org/releases/source/xbmc-$pkgver.tar.gz"
	"xbmc.service"
	"0050+0051+0057-Use-Dove-hardware-graphics-scaler.patch"
	"505_report-cpu-type-and-temp.patch"
	"${_patches[@]/#/http://hg.openbricks.org/openbricks/raw-file/default/config/platforms/arm/armada5xx/packages/xbmc/patches/}"
       "0018_fix-lib-vivante.patch")

md5sums=('7ae385ebf8e5cfcb917393235e6efbdb'
         '1b5d4a888a375df9949e4eed1e060ce5'
         'c3fec98a9a653c7d8efd6b8b159f9fde'
         'a03cc7f313791631543c8fbcbf868fc2'
         '8eacd6886116cb8c8a3130150b174988'
         '543f3482a4dfa622245e78b44e62d5d2'
         '5a7c4303fc4ffd92f0f8e880ccf0076d'
         '12bfdd9088ff5d55ddb2ab281881d0e7'
         '4dda79437b0295b90ea97693b70291e1'
         'd825bbbad9cdcc74f5bb53d51c9009cc'
         'b82517d1534042cfcbfb8eead00da0a5'
         'c5bc44468663940b7e12bd68c7203b47'
         '39fa2198f93f9529c20d28fbdf8e6cc1'
         '5c0dd38851694d15927dad21d2b6410c'
         '2d0a1b3859408c67efdf7816fc02ad4a'
         '9926708a14035d24b023b13eb52fa0ea'
         'd234509aa2de06a7f086dee7585d0e8b'
         'd7b97ba8e4b033a0f8ae82d9069ddd0c'
         'b7a6ec0ba0b1cf586a8148835ee433ed'
         '7c7dfa0ad66e40d21c9d3cf34cee6d74'
         '1b6462b42b35f21aee9a6bbf15ba5c64'
         '5ad567e5c70506d4fc5c930768bd1d5c'
         '378205587868e0e974dc50865c3c400f'
         'acb85b649ac0a7f0b35dacb99501e4ad'
         'c53ee283801c004fbc1a960921fb4521'
         '319dd19f31459d0342d5d6387831943f'
         'c748c43f11e7024b0ef527ad53e52fce'
         '0af00d3d3f726481c209d1287b3f6aab'
         '7be7215e47f916f00ce9e03aa0daf1cd'
         '5fd9f0f24741172029d9e51c97983fde'
         '04c8a05a10a9cc492abdead9ea05cb11'
         '6a2d9a3eb6472df43d8463cd75f5051d'
         '4e967b7d936d770bd153d02051960ba1'
         '9898521e8a2b4c04260a84f7a8864864'
         '09a927d16687fdd7a907ab7a2529fb1f'
         '383fdf68505b645e755cf9774f72800d'
         '7b3ce4ef4f297c25a9ca926816f544ec'
         'f3d3677267074a0e1450af3a17aa0be9'
         '702c454aabbf780bffc9f0d2ec39e189'
         '3c4cd7316c48776f694b411caf87453d'
         '3ca61d4f8a0a36f0d64597f76a2b4fed'
         'fc442d1f155cbdce2bcb1ad722670579'
         'de5a153e75280b207170b699194536b3'
         'b62ef58068a3b4c829e5c2139384473a'
         '0fbbc4c6efbe552f2fe18b5d67d4cc02'
         '43c1f47ef68e6075f2efb69a3299aa5a'
         'c26eb3393e28be352eb2a472dd35307f'
         '25e43b1d98169ba617ddbf866f844382'
         'e71788efb5c23bca01fa190606c62234'
         '2757267a5a4270ade6f07ec0c7c95d38'
         '5c5ee6c0b316adf277349f6f100dea07'
         'ab19f3b1c6f7436415c0ac559295aae4'
         'd70663fe3ccf4b2bb1d348fb4cc08a2a'
         '3668ae2f591ac60899563ce7fc480d11'
         '3fc5d78ecd818d899014bc694d1d2444'
         'd296079c26d2782b42170ebfca7367cd'
         'd4c03833471d000f5f410e9c0c1997ac'
         '0048d33a23c3bd93d5f28c7c8acbef21'
         'c083036f20ef64ee680ca8b78228aa61'
         '2adc790842f7e7d1026c6c425bb23926'
         '8e6afeda7607975526543a55716f328d'
         '2b69b0db80701aaf5276f1e5d53073e6')

build() {
  cd "${srcdir}/xbmc-$pkgver-Frodo"

  export LDFLAGS="$LDFLAGS -L/opt/marvell-libgfx/lib"
  #export CFLAGS="$CFLAGS -I/opt/marvell-libgfx/include"
  #export CXXFLAGS="$CXXFLAGS -I/opt/marvell-libgfx/include"
  INCLUDES="-I/opt/fsl/include" 
  export CFLAGS="-Ofast -mfloat-abi=hard -mfpu=vfpv3-d16 -mtls-dialect=gnu -march=armv7-a -pipe -fstack-protector --param=ssp-buffer-size=4 -D_FORTIFY_SOURCE=2 $INCLUDES -Wl,-rpath,/opt/marvell-libgfx/lib -L/opt/marvell-libgfx/lib"
  export CPPFLAGS="-Ofast -mfloat-abi=hard -mfpu=vfpv3-d16 -mtls-dialect=gnu -march=armv7-a -fstack-protector --param=ssp-buffer-size=4 -D_FORTIFY_SOURCE=2 $INCLUDES -Wl,-rpath,/opt/marvell-libgfx/lib -L/opt/marvell-libgfx/lib"
  export CXXFLAGS="-Ofast -mfloat-abi=hard -mfpu=vfpv3-d16 -mtls-dialect=gnu -march=armv7-a -fstack-protector --param=ssp-buffer-size=4 -D_FORTIFY_SOURCE=2 $INCLUDES -Wl,-rpath,/opt/marvell-libgfx/lib -L/opt/marvell-libgfx/lib"
  export TEXTUREPACKER_NATIVE_ROOT="/usr"

  sed "20,28d" -i "${srcdir}/0066-vmeta-cleanups-after-wmv3.patch"

  for _patch in "${srcdir}/"*.patch; do
    patch -p1 -i "${_patch}"
  done

  sed -r "s#(libsmbclient.h>)\$#samba-4.0/\1#" \
    -i xbmc/filesystem/SmbFile.cpp xbmc/filesystem/SMBDirectory.cpp

  # fix lsb_release dependency
  sed -i -e 's:/usr/bin/lsb_release -d:cat /etc/arch-release:' xbmc/utils/SystemInfo.cpp

  # Bootstrapping XBMC
  ./bootstrap

  # Configuring XBMC
  export PYTHON_VERSION=2  # external python v2
  ./configure --prefix=$_prefix --exec-prefix=$_prefix \
    --with-platform="marvell-dove" \
    --disable-debug \
    --enable-optimizations \
    --disable-gl \
    --enable-gles \
    --enable-sdl \
    --disable-vaapi \
    --disable-vdpau \
    --disable-xvba \
    --enable-joystick \
    --enable-xrandr \
    --enable-rsxs \
    --enable-projectm \
    --enable-x11 \
    --enable-pulse \
    --enable-rtmp \
    --enable-samba \
    --enable-nfs \
    --enable-afpclient \
    --enable-airplay \
    --enable-airtunes \
    --disable-external-ffmpeg \
    --enable-ffmpeg-libvorbis \
    --enable-dvdcss \
    --disable-hal \
    --enable-avahi \
    --enable-webserver \
    --enable-optical-drive \
    --enable-libbluray \
    --enable-texturepacker \
    --enable-udev \
    --enable-libusb \
    --enable-libcec \
    --enable-external-libraries \
    CFLAGS="${CFLAGS}" CXXFLAGS="${CXXFLAGS}"
  # Now (finally) build
  make
}

package() {
  cd "${srcdir}/xbmc-$pkgver-Frodo"
  # Running make install
  make DESTDIR="${pkgdir}" install

  # run feh with python2
  sed -i -e 's/python/python2/g' ${pkgdir}${_prefix}/bin/xbmc

  # lsb_release fix
  sed -i -e 's/which lsb_release &> \/dev\/null/\[ -f \/etc\/arch-release ]/g' "${pkgdir}${_prefix}/bin/xbmc"
  sed -i -e "s/lsb_release -a 2> \/dev\/null | sed -e 's\/\^\/    \/'/cat \/etc\/arch-release/g" "${pkgdir}${_prefix}/bin/xbmc"

  # Tools
#  install -D -m 0755 "${srcdir}/xbmc-$pkgver/xbmc-xrandr" "${pkgdir}${_prefix}/share/xbmc/xbmc-xrandr"
  install -D -m 0755 "${srcdir}/xbmc-$pkgver-Frodo/tools/TexturePacker/TexturePacker" "${pkgdir}${_prefix}/share/xbmc/"

  # Licenses
  install -d -m 0755 "${pkgdir}${_prefix}/share/licenses/${pkgname}"
  for licensef in LICENSE.GPL copying.txt; do
    mv "${pkgdir}${_prefix}/share/doc/xbmc/${licensef}" "${pkgdir}${_prefix}/share/licenses/${pkgname}"
  done

  # systemd stuff
  install -Dm0644 $srcdir/xbmc.service $pkgdir/usr/lib/systemd/system/xbmc.service
}

